package com.wframe.project.jnd.services.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.frame.database.JdataBean;
import com.wframe.project.jnd.dao.ProductDao;
import com.wframe.project.jnd.services.CacheMasterDataService;
import com.wframe.project.jnd.vo.ClsMapVo;

/**
 * <b>主数据缓存服务</b>
 * @author lenovo
 *
 */
@Service
public class CacheMasterDataServiceImpl implements CacheMasterDataService{

	private static Logger log = LoggerFactory.getLogger(CacheMasterDataServiceImpl.class);
	
	@Autowired
	ProductDao productDao;
	
	/**
	 * 加载分类导航(待后续优化)
	 */
	public List<ClsMapVo> loadClsNav(String basecode){
		Map<String,ClsMapVo> map=new LinkedHashMap<String,ClsMapVo>();
		List<JdataBean> clslist=productDao.loadHomeProductclass(basecode);
		List<JdataBean> plist=productDao.loadFeaturedProduct();
		for(JdataBean cls:clslist){
			String code=cls.getString("code");
			int len=code.split("\\.").length;
			if(len>3)continue;
			else if(len<3){
				ClsMapVo vo1=map.get(cls.getString("id"));
				if(vo1==null){
					vo1=new ClsMapVo(cls.getString("id"),cls.getString("name"),cls.getString("code"),cls.getInteger("orderno"));
				} else {
					vo1.setId(cls.getString("id"));
					vo1.setName(cls.getString("name"));
					vo1.setCode(cls.getString("code"));
					vo1.setOrderno(cls.getInteger("orderno"));
				}
				vo1.setPhoto(cls.getString("photo"));
				map.put(cls.getString("id"), vo1);
			} else {
				String pid=cls.getString("pid");
				ClsMapVo vo1=map.get(pid);
				List<ClsMapVo> sublist=null;
				if(vo1==null){
					vo1=new ClsMapVo();
					sublist=vo1.getSublist();
				} else{
					sublist=vo1.getSublist();
				}
				if(sublist==null)sublist=new ArrayList<ClsMapVo>();
				ClsMapVo subvo=new ClsMapVo(cls.getString("id"),cls.getString("name"),cls.getString("code"),cls.getInteger("orderno"));
				subvo.setPhoto(cls.getString("photo"));
				subvo.setSublist(new ArrayList<ClsMapVo>());
				for(JdataBean pro:plist){
					if(cls.getString("code").equals(pro.getString("classcode"))){
						subvo.getSublist().add(new ClsMapVo(pro.getString("id"),pro.getString("productname"),pro.getString("photoview")));
					}
				}
				sublist.add(subvo);
				vo1.setSublist(sublist);
				map.put(pid, vo1);
			}
		}
		ArrayList<ClsMapVo> list=new ArrayList<ClsMapVo>(map.values());
		Collections.sort(list, new Comparator<ClsMapVo>(){ 
            public int compare(ClsMapVo o1, ClsMapVo o2) {
                if(o1.getOrderno()<o2.getOrderno()){  
                    return -1;  
                }  
                if(o1.getOrderno() == o2.getOrderno() && Integer.parseInt(o1.getId())<Integer.parseInt(o2.getId())){  
                    return -1;  
                }  
                return 1;  
            }  
        });
		return list;
	}
	
	private ClsMapVo getcls(List<ClsMapVo> clslist,String id,String name,String code,Integer orderno){
		ClsMapVo getvo=null;
		for(ClsMapVo vo:clslist){
			if(vo.getId().equals(id)){
				getvo=vo;
				break;
			}
		}
		if(getvo==null){
			getvo=new ClsMapVo(id,name,code,orderno);
			int index=clslist.size();
			for(int i=0;i<clslist.size();i++){
				if(getvo.getOrderno()<clslist.get(i).getOrderno()){
					index=i;
					break;
				}else if(getvo.getOrderno()==clslist.get(i).getOrderno() && Integer.parseInt(getvo.getId())<Integer.parseInt(clslist.get(i).getId())){
					index=i;
					break;
				}
			}
			clslist.add(index,getvo);
		}
		return getvo;
	}
}
